package com.moons.uvcdemo.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class ARUtil {

    public static final int FACE_FEATURE_LENGHT = 512;
    public static float calcCosValue_accel(float[] v1, float v1_len, float[] v2, float v2_len, int length) {
        float top = 0;
        for (int i = 0; i < length; i++) {
            top += v1[i] * v2[i];
        }
        if (v1_len == 0 || v2_len == 0) {
            return Float.MIN_NORMAL;
        }
        return top / (v1_len * v2_len);
    }

    public static byte[] floatArr2byteArr(float[] fArr){
        ByteArrayOutputStream bas = new ByteArrayOutputStream();
        DataOutputStream ds = new DataOutputStream(bas);
        for (float f : fArr) {
            try {
                ds.writeFloat(f);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bas.toByteArray();
    }

    public static float[] byteArr2floatArr(byte[] bArr){
        ByteArrayInputStream bas = new ByteArrayInputStream(bArr);
        DataInputStream ds = new DataInputStream(bas);
        float[] fArr = new float[bArr.length / 4];  // 4 bytes per float
        for (int i = 0; i < fArr.length; i++)
        {
            try {
                fArr[i] = ds.readFloat();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fArr;
    }
}
